package com.realbrand.net.realbrandpay.ui.activity.wallet.set;

import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/11/10.
 */

public class AboutActivity extends BaseActivity {
    @BindView(R.id.aboutActivity_toolbar)
    Toolbar aboutActivityToolbar;
    @BindView(R.id.aboutActivity_tv_share)
    TextView aboutActivityTvShare;
    @BindView(R.id.aboutActivity_tv_isUpData)
    Button aboutActivityTvIsUpData;
    @BindView(R.id.aboutActivity_tv_versionName)
    TextView aboutActivityTvVersionName;

    public  String versionUrl;

    public int VersionUpData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        aboutActivityToolbar.setTitle("");
        aboutActivityToolbar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(aboutActivityToolbar);
        aboutActivityToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        aboutActivityTvIsUpData.setEnabled(false);
        aboutActivityTvIsUpData.setText("已是最新");
        //获取本地版本名
        try {
            String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            aboutActivityTvVersionName.setText("版本号"+versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        //检查是否有新本版
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.shareName, MODE_PRIVATE);
        versionUrl = sharedPreferences.getString(Constant.VersionUpDataUrl, "");
        int VersionBenDi = sharedPreferences.getInt(Constant.VersionBenDi, 0);
        VersionUpData = sharedPreferences.getInt(Constant.VersionUpData, 0);
        if (VersionBenDi < VersionUpData) {
            aboutActivityTvIsUpData.setText("发现新版本");
            aboutActivityTvIsUpData.setBackgroundColor(getResources().getColor(R.color.red));
            aboutActivityTvIsUpData.setEnabled(true);
        }
        setOnClick();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.about_activity;
    }

    @Override
    protected void setOnClick() {
        aboutActivityTvShare.setOnClickListener(this);
        aboutActivityTvIsUpData.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击分享
            case R.id.aboutActivity_tv_share:
                break;
            //点击更新
            case R.id.aboutActivity_tv_isUpData:
                L.d(L.TAG,"点击更新"+versionUrl);
                Static.upDataAPP(versionUrl, VersionUpData, AboutActivity.this);
                aboutActivityTvIsUpData.setEnabled(false);
                break;
        }
    }
}
